<?php
namespace App\Admin\Controllers;

use App\Models\News;
use App\Models\NewsCat;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class NewsController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(News::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->journal()->name_short();
            $grid->newscat()->name('栏目');
            $grid->title('标题');
            $grid->updated_at('提交时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(News::class, function (Form $form) {
            $newscat = NewsCat::all();
            $new_cat = [];
            foreach ($newscat as $j) {
                $new_cat[$j->id] = $j['name'];
            }
            $form->display('id', 'ID');
            $journal_id = [
                0 => '--请选择期刊--',
                1 => '动物学研究',
            ];
            $form->select('journal_id', '期刊')->options($journal_id)->default(0)->rules('required');
            $form->select('newscat_id', '所属栏目')->options($new_cat)->rules('required');
            $form->text('title', '标题')->rules('required');
            $form->checkbox('is_hot ')->options([1 => '置顶']);
            $form->checkbox('is_checked ')->options([1 => '审核'])->default(1);
            $form->checkbox('is_newest ')->options([1 => '是否最新']);
            $form->textarea('introduction', '简介');
            $form->text('art_source', '文章来源');
            $form->text(' url ', '链接地址');
            // $form->image('picture', '文章图片')->uniqueName();
             $form->multipleImage('picture', '文章图片');
            $form->file('attachment', '文章附件');
            $form->text('keywords', '关键词(用"|"隔开)');
            $form->editor('content', '文章正文');
            $form->date('published_date', '发布时间');
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}